home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ARASAN_S.ZIP / BOARD.H < prev    next >
C/C++ Source or Header  |  1994-08-14  |  5KB  |  206 lines

  1. // Copyright 1994 by Jon Dart.  All Rights Reserved.
  2.  
  3. #ifndef _BOARD_H
  4. #define _BOARD_H
  5.  
  6. #include "types.h"
  7. #include "square.h"
  8. #include "piece.h"
  9. #include "attacks.h"
  10. #include "material.h"
  11. #include <iostream.h>
  12.  
  13. class ExtendedMove;
  14. class ReversibleMove;
  15.  
  16. class Board
  17. {
  18.    // This class encapsulates the chess board, and provides functions
  19.    // for making and undoing moves.         
  20.  
  21. public:    
  22.  
  23.    friend class ReversibleMove;
  24.    
  25.    enum CheckStatusType { InCheck, NotInCheck };
  26.  
  27.    enum CastleType { CanCastleEitherSide,
  28.                  CanCastleKSide,
  29.              CanCastleQSide,
  30.              CastledKSide,
  31.                  CastledQSide,
  32.              CantCastleEitherSide};
  33.       
  34.    Board();
  35.    // returns a board set up in its initial position, White to move.
  36.        
  37.   ~Board();
  38.  
  39.    void Reset();
  40.    // resets it to initial position
  41.        
  42. #ifdef RANGE_CHECK
  43.    const Piece &operator[]( const Square sq ) const;
  44. #else
  45.    const Piece &operator[]( const Square sq ) const
  46.    {
  47.        return my_Contents[sq];
  48.    }
  49. #endif
  50.  
  51.    void Set_Contents( const Piece &p, const Square sq )
  52.    {
  53.        my_Contents[sq] = p;
  54.    }
  55.    
  56.    Piece GetPiece( const Square start, const int dir, Square &dest ) const;
  57.    // returns 1st piece in the given direction from "start".  Also 
  58.    // sets "dest", where it is
  59.  
  60.    CastleType CastleStatus( const ColorType side ) const
  61.    {
  62.        return my_CastleStatus[side];        
  63.    }
  64.    
  65.    void Set_CastleStatus( const CastleType t, const ColorType side )
  66.    {
  67.        my_CastleStatus[side] = t;
  68.    }
  69.  
  70.    ColorType Side() const
  71.    // side to move       
  72.    {
  73.        return my_Side;         
  74.    }
  75.    
  76.    ColorType OppositeSide() const
  77.    {
  78.        return OppositeColor(my_Side);
  79.    }
  80.    
  81.    void Set_Side( const ColorType side )
  82.    {
  83.        my_Side = side;
  84.    }
  85.    
  86.    const Square& PiecePos( const ColorType side, const int i ) const
  87.    {
  88. #ifdef RANGE_CHECK
  89.        assert((i>=0) && (i<16));
  90. #endif
  91.        return my_PiecePos[i][side];
  92.    }
  93.    
  94.    const Material &getMaterial( const ColorType side ) const
  95.    {
  96.        return my_Material[side];
  97.    }
  98.    
  99.    byte PFileCount( const int rank, const ColorType side ) const
  100.    {
  101.        return my_PFileCount[rank][side];
  102.    }
  103.  
  104.    byte RFileCount( const int rank, const ColorType side ) const
  105.    {
  106.        return my_RFileCount[rank][side];
  107.    }
  108.  
  109.    const Square EnPassantSq( const ColorType side ) const
  110.    {
  111.        return my_EnPassantSq[side];    
  112.    }
  113.        
  114.    void Set_EnPassantSq( const Square sq, const ColorType side )
  115.    {
  116.        my_EnPassantSq[side] = sq;
  117.    }
  118.        
  119.    CheckStatusType CheckStatus( ) const;
  120.  
  121.    const Square &KingPos( const ColorType side ) const
  122.    {
  123.         return my_KingPos[side];       
  124.    }
  125.        
  126.    unsigned long HashCode() const
  127.    {
  128.        return my_HashCode;
  129.    }
  130.    
  131.    int operator == ( const Board &ABoard )
  132.    {
  133.        return my_HashCode == ABoard.HashCode();
  134.    }
  135.    
  136.    int Direction( const Square &sq1, const Square &sq2 ) const;
  137.    // If sq1 and sq2 are in a straight line, return its direction
  138.    // (increment by which a piece at sq1 could move to sq2).  Other-
  139.    // wise, return 0.       
  140.    
  141.    int Between( const Square &sq1, const Square &sq2, 
  142.             Square *squares) const;
  143.    // fills "squares" with a list of squares between sq1 and sq2 (in
  144.    // a straight line), returns the # of squares.
  145.    
  146.    Boolean Clear( const Square &sq1, const Square &sq2 ) const;
  147.    // returns "true" if there is a clear path between sq1 and sq2
  148.  
  149.    void MakeMove( const ExtendedMove &move );
  150.    // makes a move
  151.  
  152.    void UndoMove( const ReversibleMove &rmove );
  153.    // undoes a previous move.
  154.        
  155.    unsigned num_attacks( const Square &sq, const ColorType side) const
  156.    {
  157.        return my_attacks.num_attacks(sq,side);
  158.    }
  159.        
  160.    const Attack_Entry &get_attacks( const Square sq, const ColorType side ) const
  161.    { 
  162.         return my_attacks.get_attacks(sq,side);
  163.    }
  164.  
  165.    unsigned pawn_attacks( const Square &sq, const ColorType side) const
  166.    {
  167.        return my_attacks.pawn_attacks(sq,side);
  168.    }
  169.        
  170.    friend istream & operator >> (istream &i, Board &board);
  171.    friend ostream & operator << (ostream &o, Board &board);
  172.  
  173. #if !defined(WINDOWS) && defined(DEBUG_ATTACKS)
  174.    // for debugging :
  175.    void dump_attacks()
  176.    {
  177.        my_attacks.dump_attacks();        
  178.    }
  179. #endif   
  180.    
  181.    private:
  182.        
  183.    Piece my_Contents[64];
  184.    ColorType my_Side;
  185.    Square my_PiecePos[16][2];
  186.    Square my_KingPos[2];
  187.    Material my_Material[2];
  188.    byte my_PFileCount[10][2];
  189.    byte my_RFileCount[10][2];
  190.    Square my_EnPassantSq[2];
  191.    CastleType my_CastleStatus[2];
  192.    unsigned long my_HashCode;
  193.    Attacks my_attacks;
  194.    
  195.    void set_secondary_vars();
  196.    // update all fields of the board, based on the piece positions.
  197.  
  198.    void UpdateCastleStatus( const ColorType side, 
  199.                          const Square sq );
  200.    void undo_castling(const Square &kp,const Square &oldkingsq,
  201.        const Square &newrooksq, const Square &oldrooksq);
  202. };
  203.   
  204. #endif
  205.  
  206.